package xredis

import (
	"context"
	"github.com/go-redis/redis"
)

// RedisPipelineClient by RedisClient get pipeline
type RedisPipelineClient struct {
	*RedisCmd
	client redis.Pipeliner
}

func NewRedisPipelineClient(ctx context.Context, redisConf *RedisConfig) (*RedisPipelineClient, error) {
	redisClient, err := NewRedisClient(ctx, redisConf)
	if err != nil || redisClient == nil {
		return nil, err
	}
	return redisClient.Pipeline(ctx)
}

func (m *RedisPipelineClient) Exec(ctx context.Context) (cmds []redis.Cmder, err error) {
	command := "RedisPipelineClient.Exec"
	m.WrapperLog(ctx, command, func() error {
		cmds, err = m.client.Exec()
		return err
	})
	return
}

func (m *RedisPipelineClient) Discard(ctx context.Context) (err error) {
	command := "RedisPipelineClient.Discard"
	m.WrapperLog(ctx, command, func() error {
		m.client.Discard()
		return nil
	})
	return
}

func (m *RedisPipelineClient) GetRawClient(ctx context.Context) redis.Pipeliner {
	return m.client
}
